package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.ViewQuery;;


@Repository
public class ViewQueryDaoImpl {
	@Autowired
	SessionFactory sessionFactory;
	
	public void addViewQuery(ViewQuery viewQuery) {
		try{
		sessionFactory.getCurrentSession().save(viewQuery);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public List<ViewQuery> getViewQueries(Integer viewId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from ViewQuery where viewId.id = :viewId");
		query.setParameter("viewId", viewId);
		@SuppressWarnings("unchecked")
		List<ViewQuery>  viewQueries= query.list();
		if(viewQueries != null && viewQueries.size() > 0)
		return viewQueries;
		else
		return new ArrayList<ViewQuery>();
		
	}
	public ViewQuery getViewQueryById(Integer Id){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from ViewQuery where  id = :Id");
		query.setParameter("Id", Id);
		@SuppressWarnings("unchecked")
		List<ViewQuery>  viewQuery= query.list();
		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+viewQuery.size());
		if(viewQuery.size() > 0)
		return viewQuery.get(0);
		else
		return null;
		
	}

	public void saveOrupdate(ViewQuery viewQuery){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(viewQuery);
	}
	
	public ViewQuery loadViewQuery(Integer id) {
		ViewQuery viewQuery = (ViewQuery)sessionFactory.getCurrentSession().get(ViewQuery.class, id);
		return viewQuery;
		
	}
	
	public void deleteViewQuery(ViewQuery viewQuery) {
		sessionFactory.getCurrentSession().delete(viewQuery);
		
	}

}
